//SharePoint Error Reporting
//By Alon Havivi(havivi@gmail.com, http://havivi.blogspot.com/) 
//For full source code and Terms Of Use,
//visit http://ser.codeplex.com/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using Microsoft.SharePoint.Utilities;
using System.Web;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.SharePoint.Administration;

namespace SER
{
    public class SERCodeBehind : Page
    {
       
        protected TextBox txtMessage;
        protected Button btnSendFeedback;
        protected Label lblFramework;
        protected Label lblUrlReferrer;
        protected Label lblIP;
        protected Label lblPC;
        protected Label lblBrowser;
        protected Label lblWebfront;
        protected Label lblUsername;
        protected Label lblName;
        protected Label lblBrowserSupports;
        protected Label lblProbleem;
        protected Label lblApplications;
        protected Label lblProbleemTitle;
        protected Label lblLanguage;
        protected Label lblSiteReferrer;

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (Request.QueryString["err"] != null)
                {
                    lblProbleem.Text = Request.QueryString["err"].ToString();
                }
                else
                {
                    lblProbleemTitle.Visible = false;
                }
                lblWebfront.Text = Server.MachineName;
                lblUsername.Text = SPContext.Current.Web.CurrentUser.LoginName;
                lblName.Text = SPContext.Current.Web.CurrentUser.Name;
                lblSiteReferrer.Text = SPContext.Current.Web.Title;
                lblUrlReferrer.Text = SPContext.Current.Web.Url;
                lblPC.Text = GetOS(Request.ServerVariables["HTTP_USER_AGENT"]);
                lblFramework.Text = GetNETFramework(Request.ServerVariables["HTTP_USER_AGENT"]);
                lblIP.Text = Request.ServerVariables["REMOTE_ADDR"];
                lblBrowser.Text = Request.Browser.Type;
                lblBrowserSupports.Text = GetBrowserSupports();
                lblApplications.Text = GetApplications(Request.ServerVariables["HTTP_ACCEPT"]);
                lblLanguage.Text = Thread.CurrentThread.CurrentCulture.DisplayName;

            }
            catch
            {
                lblProbleemTitle.Visible = true;
                lblProbleem.Text = "unavailable";
            }

        }

        private string GetBrowserSupports()
        {
            string webSupports = "";
            if (Request.Browser.Frames)
            {
                webSupports += "Frames, ";
            }
            if (Request.Browser.Tables)
            {
                webSupports += "Tables, ";
            }
            if (Request.Browser.Cookies)
            {
                webSupports += "Cookies, ";
            }
            if (Request.Browser.VBScript)
            {
                webSupports += "VBScript, ";
            }
            if (Request.Browser.JavaScript)
            {
                webSupports += "JavaScript, ";
            }
            if (Request.Browser.JavaApplets)
            {
                webSupports += "JavaApplets, ";
            }
            if (webSupports.Length > 0)
            {
                webSupports = webSupports.Substring(0, webSupports.Length - 3);
            }
            return webSupports;
        }

        protected void btnSendFeedback_Click(object sender, EventArgs e)
        {
            try
            {

                SPUser user = SPContext.Current.Web.CurrentUser;
                SPFieldUserValueCollection userColl = new SPFieldUserValueCollection();
                userColl.Add(new SPFieldUserValue(SPContext.Current.Web, user.ID, user.Name));

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    SPWebApplication webApp = SPAdministrationWebApplication.Local;

                    using (SPSite site = webApp.Sites[0])
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;

                            SPList list = web.Lists["Error Reporting"];
                            SPListItemCollection listItems = list.Items;
                            SPListItem item = listItems.Add();
                            item["Title"] = "#ERR" + DateTime.Now.ToString();
                            item["User"] = userColl;
                            item["Username"] = lblUsername.Text;
                            item["PC"] = lblPC.Text;
                            item["IP"] = lblIP.Text;
                            item[".NET Framework"] = lblFramework.Text;
                            item["Browser"] = lblBrowser.Text;
                            item["Browser Supports"] = lblBrowserSupports.Text;
                            item["Applications"] = lblApplications.Text;
                            item["Language"] = lblLanguage.Text;
                            item["Webfront"] = lblWebfront.Text;
                            SPFieldUrlValue urlFieldValue = new SPFieldUrlValue();
                            urlFieldValue.Description = lblUrlReferrer.Text;
                            urlFieldValue.Url = lblUrlReferrer.Text;
                            item["Url Referrer"] = urlFieldValue;
                            item["Site Referrer"] = lblSiteReferrer.Text;
                            item["Error Description"] = lblProbleem.Text;
                            item["User Remarks"] = txtMessage.Text;
                            item.Update();
                        }
                    }
                   
                    SPUtility.TransferToSuccessPage("Thank you for helping us to improve the site");

                });
            }
            catch
            {
                SPUtility.TransferToSuccessPage("Thank you for helping us to improve the site");
            }
        }

        private string GetOS(string ua)
        {
            string os = "Onbekend";
            string cpu = Request.ServerVariables["HTTP_UA_CPU"];
            if (ua.Contains("Win16"))
            {
                return "Windows 3.11";
            }
            else if (ua.Contains("Windows 95") || ua.Contains("Win95") || ua.Contains("Windows_95"))
            {
                return "Windows 95";
            }
            else if (ua.Contains("Windows 98") || ua.Contains("Win98"))
            {
                return "Windows 98";
            }
            else if (ua.Contains("Windows NT 5.0") || ua.Contains("Windows 2000"))
            {
                return "Windows 2000";
            }

            else if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows XP"))
            {
                return "Windows XP";
            }
            else if (ua.Contains("Windows NT 5.2"))
            {
                return "Windows Server 2003";
            }
            else if (ua.Contains("Windows NT 6.0") && (cpu == "x86"))
            {
                return "Windows Vista";
            }
            else if (ua.Contains("Windows NT 6.0"))
            {
                return "Windows XP x64";
            }
            else if (ua.Contains("Windows NT 7.0"))
            {
                return "Windows 7";
            }
            else if (ua.Contains("Windows NT 4.0") || ua.Contains("WinNT4.0") || ua.Contains("WinNT") || ua.Contains("Windows NT"))
            {
                return "Windows NT 4.0";
            }
            else if (ua.Contains("Windows ME"))
            {
                return "Windows ME";
            }
            else if (ua.Contains("OpenBSD"))
            {
                return "Open BSD";
            }
            else if (ua.Contains("SunOS"))
            {
                return "Sun OS";
            }
            else if (ua.Contains("Linux") || ua.Contains("X11"))
            {
                return "Linux";
            }
            else if (ua.Contains("Mac_PowerPC") || ua.Contains("Macintosh"))
            {
                return "Mac OS";
            }
            else if (ua.Contains("OS/2"))
            {
                return "OS/2";
            }


            return os;
        }

        private string GetApplications(string ha)
        {
            
            string webApplications = "";
            if (ha.Contains("application/vnd.ms-excel"))
            {
                webApplications += "Excel, ";
            }
            if (ha.Contains("application/vnd.ms-powerpoint"))
            {
                webApplications += "PowerPoint, ";
            }
            if (ha.Contains("application/msword"))
            {
                webApplications += "Word, ";
            }
            if (ha.Contains("application/x-shockwave-flash"))
            {
                webApplications += "Flash, ";
            }
            if (ha.Contains("application/x-silverlight"))
            {
                webApplications += "SilverLight, ";
            }

            if (webApplications.Length > 0)
            {
                webApplications = webApplications.Substring(0, webApplications.Length - 2);
            }
            return webApplications;
        }

        private string GetNETFramework(string ua)
        {
            string frameworks = "";

            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(".NET CLR [0-9.]*");
            bool flag = false;
            foreach (Match m in r.Matches(ua))
            {
                string tmpString = m.Value.Replace("CLR", "");
                tmpString = tmpString.Replace(".NET", "");
                if (!frameworks.Contains(tmpString.Substring(1, 4)))
                {
                    frameworks += tmpString.Substring(1, 4) + ", ";
                }
                flag = true;
            }
            if (!flag)
            {
                frameworks = "No .NET frameworks installed";
            }
            else
            {
                if (frameworks.Length > 0)
                {
                    frameworks = frameworks.Substring(1, frameworks.Length - 3);
                }
            }
            return frameworks;
        }
    }
}